﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
using UpdaterForm.Annotations;
using UpdaterForm.CustomTreeView;

namespace UpdaterForm
{
    public class UpdateInfo : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public string Name { get; set; }
        public long Size { get; set; }
        public string SizeFormed { get; private set; }
        public int Lenght { get; set; }
        public string FullName { get; set; }
        public string Message { get; set; }
        public List<UpdateTreeView> Content { get; set; }
        public UpdateLog Log { get; set; }
        public Boolean Custom { get; set; }

        private UpdateState _isInstalled;
        public UpdateState IsInstalled
        {
            get { return _isInstalled; }
            set
            {
                _isInstalled = value;
                OnPropertyChanged("IsInstalled");
            }
        }
 

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public UpdateInfo()
        {
            
        }

        public UpdateInfo(DirectoryInfo dir)
        {
            FullName = dir.FullName;
            if (dir.Parent != null) 
                Name = dir.Parent.Name;
            Size = DirSize(dir);
            Lenght =  dir.GetFiles( "*", SearchOption.AllDirectories).Length;
            SizeFormed = ToBytes(Size);
            //TODO: mozet est vozmoznost srazu stavit pravelnoe znachenie a ne default 
           IsInstalled = UpdateState.RecentlyAdded;
            Message = ReadUpdateMessage();
            Content = UpdateTreeView.LoadFromDirInfo(dir);
            Log = new UpdateLog{ Name = Name };
            Custom = false;
        }

        private string ReadUpdateMessage()
        {
            var fileName = Path.Combine(FullName, AppCahce.Conf.MessageFileName);
            try
            {
                return File.ReadAllText(fileName);

            }
            catch (Exception)
            {
                return String.Empty;
            }
        }

        private static long DirSize(DirectoryInfo d)
        {
            var fis = d.GetFiles();
            var Size = fis.Sum(fi => fi.Length);
            var dis = d.GetDirectories();
            Size += dis.Sum(di => DirSize(di));
            return (Size);
        }

        //TODO: neverojatnoe gavno,nado peredelat
        private static string ToBytes(long bytes)
        {
            IList<string> units = new List<string>{ "B", "KB", "MB", "GB", "TB" };
            var pow = Math.Floor((bytes > 0 ? Math.Log(bytes) : 0) / Math.Log(1024));
            pow = Math.Min(pow, units.Count - 1);
            var value = bytes / Math.Pow(1024, pow);
            return value.ToString(pow == 0 ? "F0" : "F" + 2) + " " + units[(int)pow];
        }



      
    }
}
